const regeneratorRuntime = require("../../utils/runtime.js")
const login = require('../../utils/utils.js')
const {
  _post,
  _get
} = require('../../utils/request.js')
Page({
  data: {
    value: '',
    mdnList: [],
    loading: false,
    page: 1,
    hasSearch: false,
    lastSearch: ''
  },

  async onLoad() {},

  async getMDNList() {
    this.setData({
      loading: true,
      hasSearch: true,
    })
    let res = await _get('/mdn', {
      name: this.data.lastSearch,
      page: this.data.page
    })

    if (res.code === 0) {
      if (this.data.mdnList.length > 0) {
        this.setData({
          mdnList: this.data.mdnList.concat(res.data)
        })
      } else {
        this.setData({
          mdnList: res.data
        })
      }
    }
    this.setData({
      loading: false
    })
  },

  searchOnChange(e) {
    this.setData({
      value: e.detail,
    })
  },

  async onSearch(e) {
    this.setData({
      value: e.detail
    })
    if (this.data.value) {
      this.setData({
        page: 1,
        mdnList: [],
        lastSearch: this.data.value
      })
      await this.getMDNList()
    }
  },

  handleMDNItemClick(e) {
    wx.navigateTo({
      url: '/pages/subject/subject?url=' + encodeURIComponent(e.currentTarget.dataset.url),
    })
  },

  async handleLoadNextPage() {
    if (!this.data.loading && this.data.hasSearch) {
      this.setData({
        page: this.data.page + 1
      })

      await this.getMDNList()
    }
  }
})